/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.web.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.Role;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.mapper.UserMapper;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.web.entity.WxAgents;
import com.jeeplus.modules.web.mapper.WxAgentsMapper;

/**
 * 代理商Service
 * @author rongjie
 * @version 2017-08-06
 */
@Service
@Transactional(readOnly = true)
public class WxAgentsService extends CrudService<WxAgentsMapper, WxAgents> {

	@Autowired
	WxAgentsMapper mapper;
	@Autowired
	private UserMapper userMapper;

	public WxAgents get(String id) {
		return super.get(id);
	}

	public WxAgents getByUsername(String username) {
		return mapper.getByUsername(username);
	}

	public List<WxAgents> getByUsernameWithoutSelf(String username, String id) {
		return mapper.getByUsernameWithoutSelf(username,id);
	}

	/**
	 * 根据代理商层级，查询代理商列表
	 * @param level
	 * @param status 1 已审核通过，0 未审核  null 不区分
	 * @return
	 */
	public List<WxAgents> getByLevel(int level, String status) {
		return mapper.getByLevel(level, status);
	}

	/**
	 * 查询代理商层级大于0且小于level的代理商
	 * @param level
	 * @param status 1 已审核通过，0 未审核  null 不区分
	 * @return
	 */
	public List<WxAgents> getByParentsLevel(int level, String status) {
		return mapper.getByParentsLevel(level, status);
	}

	public List<WxAgents> findList(WxAgents wxAgents) {
		return super.findList(wxAgents);
	}
	public List<WxAgents> findListMore(WxAgents wxAgents) {
		return mapper.findListMore(wxAgents);
	}

	public Page<WxAgents> findPage(Page<WxAgents> page, WxAgents wxAgents) {
		return super.findPage(page, wxAgents);
	}
	public Page<WxAgents> findPageMore(Page<WxAgents> page, WxAgents wxAgents) {
		dataRuleFilter(wxAgents);
		wxAgents.setPage(page);
		page.setList(mapper.findListMore(wxAgents));
		return page;
	}

	@Transactional(readOnly = false)
	public void save(WxAgents wxAgents) {
		String userName = "";
		if(StringUtils.isNotBlank(wxAgents.getId())){
			userName = mapper.get(wxAgents.getId()).getUserName();
		}

		// 绑定上级代理商
		if (wxAgents.getParentId() != null){
			WxAgents parent = mapper.get(wxAgents.getParentId()+"");
			// 最多绑定3级
			if (StringUtils.isNotBlank(parent.getParentIds())) {
				String[] parentIds = parent.getParentIds().split(",");
				if (parentIds.length >= 3){
					wxAgents.setParentIds(parentIds[1]+","+parentIds[2]+","+parent.getId());
				}else {
					wxAgents.setParentIds(parent.getParentIds()+","+parent.getId());
				}
			}
		}
		super.save(wxAgents);
		User user = userMapper.findUniqueByProperty("login_name", userName);
		if (user==null) {
			// 新增user表信息，用于登录使用
			user = new User();
			user.setMobile(wxAgents.getUserName());
			user.setLoginName(wxAgents.getUserName());
			user.setPassword(SystemService.entryptPassword(wxAgents.getPassword()));
			user.setCompany(new Office("1"));
			user.setLoginDate(new Date());
			user.preInsert();
			userMapper.insert(user);
		}else{
			//通过mobile关联查询到需要更新的user记录
			//取wxAgents的userName和password同步到sys_user
			user.setMobile(wxAgents.getUserName());
			user.setLoginName(wxAgents.getUserName());
			user.setPassword(SystemService.entryptPassword(wxAgents.getPassword()));
			user.preUpdate();
			userMapper.update(user);
		}
	}

	@Transactional(readOnly = false)
	public void delete(WxAgents wxAgents) {
		super.delete(wxAgents);
	}

}